import Vue from 'vue';
import ToastView from './index.vue';

const Toast = Vue.extend(ToastView);

/**
 * 展示toast
 * @param {Number} duration 消失时间（如是加载状态则无效）
 * @param {String} message 展示信息
 */
function toast({
  duration = 3000,
  message = '',
  btnText,
  btnOnClick
}) {
  let instance = new Toast({
    data: {
      message,
      btnText,
      btnOnClick
    }
  });

  instance.close = () => {
    document.body.removeChild(instance.$el);
    instance.$destroy();
    instance = null;
  };

  instance.$mount();
  document.body.appendChild(instance.$el);

  setTimeout(() => {
    instance.close();
  }, duration);

  return instance;
}

export default toast;
